#include "user.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
/*
    Create_User_LinkedList:创建一条空用户信息链表
*/
struct H_User*Create_User_LinkedList()
{
    struct H_User* h = malloc(sizeof(*h));
    if(h == NULL)
        return NULL;
    h->first = h->last = NULL;
    h->num = 0;
    return h;
}

bool Insert_User_Node(struct H_User*h,char* user_name, char*user_pwd)
{
    if(h == NULL)
        return false;
    struct User_Node*p = h->first;
    while(p)
    {
        //用户名已存在
        if(strcmp(p->name,user_name) == 0 )
        {
            return false;
        }
        p = p->next;
    }
    
    //创建新节点，并赋值
    struct User_Node*pnew = malloc(sizeof(*pnew));
    strcpy(pnew->name ,user_name);
    strcpy(pnew->pwd, user_pwd);
    pnew->next = NULL;

    //增加链表
    if(h->first == NULL && h->last == NULL && h->num == 0)
    {
        h->first = h->last = pnew;
    }
    else
    {
        h->last->next = pnew;
        h->last = pnew;
    }

    h->num++;

    return true;

}